﻿using System;

using System.Data;
using System.Data.OracleClient;
using System.Windows.Forms;

namespace HelloWorld
{
    class MyOracleConnection
    {
        private static  string _connectionString = null;

        #region 构造函数重载
        public MyOracleConnection ()
        {

            _connectionString = "Data Source=orcl; User Id=test1; Password=212310;" +
                               "Persist Security Info=True;";
            //连接字符串,Data Source 是指数据库名字.如我用的是本机的Oracle
            //的数据库,名字为LiPu. user id 是     
            //用户名,你可以用System 或是你自己添加的一个用户.Password是
            //对应用户的密码.
        }

        public MyOracleConnection(string myconnectionString)
        {
            _connectionString = myconnectionString;
        }

        #endregion

        #region 测试
       
        public void doConnect ()
        {
            OracleConnection conn = null;

             try
            {
                 conn = new OracleConnection(_connectionString); 
                 conn.Open();    //打开连接
                 OracleCommand cmd = conn.CreateCommand();

                 cmd.CommandText = "select * from test1.t_user";    //SQL语句
                 OracleDataReader rs = cmd.ExecuteReader();
                //读取数据，如果rs.Read()返回为false的话，就说明到记录集的尾部了
                while(rs.Read())   
                {
                     MessageBox.Show(rs.GetString(1));
                }
 
                rs.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                   
            }
        

        }
        #endregion

        #region 公共调用函数（静态）
        /// <summary>   
        /// 执行查询语句，返回OracleDataReader ( 注意：调用该方法后，一定要对OracleDataReader进行Close )   
        /// </summary>   
        /// <param name="strSQL">查询语句</param>   
        /// <returns>OracleDataReader</returns>   
        public static OracleDataReader ExecuteReader(string strSQL)
        {
            OracleConnection connection = new OracleConnection(_connectionString);
            OracleCommand cmd = new OracleCommand(strSQL, connection);
            try
            {
                connection.Open();
                OracleDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return myReader;
            }
            catch (OracleException e)
            {
                throw e;
            }
        }
        /// <summary>   
        /// 执行查询语句，返回DataSet   
        /// </summary>   
        /// <param name="SQLString">查询语句</param>   
        /// <returns>DataSet</returns>   
        public static DataSet Query(string SQLString,string table)
        {
            using (OracleConnection connection = new OracleConnection(_connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    OracleDataAdapter oda = new OracleDataAdapter(SQLString, connection);
                    oda.Fill(ds, table);
                    
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }


        #endregion

        #region 静态、执行sql语句的函数 增删改

        private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, OracleParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
                cmd.Transaction = trans;
            cmd.CommandType = CommandType.Text;//cmdType;   
            if (cmdParms != null)
            {
                foreach (OracleParameter parameter in cmdParms)
                {
                    if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
                        (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(parameter);
                }
            }
        }


        /// <summary>   
        /// 执行SQL语句，返回影响的记录数   
        /// </summary>   
        /// <param name="SQLString">SQL语句</param>   
        /// <returns>影响的记录数</returns>   
        public static int ExecuteSql(string SQLString, params OracleParameter[] cmdParms)
        {
            using (OracleConnection connection = new OracleConnection(_connectionString))
            {
                using (OracleCommand cmd = new OracleCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        return rows;
                    }
                    catch (System.Data.OracleClient.OracleException  e)
                    {
                        throw e;
                    }
                }
            }
        }

       

        #endregion

        
        
    }
}
